﻿using System;
using System.Data.Objects;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using DataAccess.DB;
using DuocThaiBinh.Common;

namespace DuocThaiBinh.Controllers
{
    public class HomeController : Controller
    {
        private db_YTeTBEntities db = new db_YTeTBEntities();

        private decimal TongThanhtoanTrongThang(int thang)
        {
            decimal totalThang = 0;
            var xuatKhos = db.XuatKhoes.Where(p => p.NgayXuatKho.Value.Month == thang && p.NgayXuatKho.Value.Year == DateTime.Now.Year);
            foreach (XuatKho xk in xuatKhos)
            {
                totalThang += Util.ConvertCurrencyToMoney(xk.TongThanhToan);
            }
            return totalThang;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ThongKe()
        {
            ViewBag.thang1 = TongThanhtoanTrongThang(1);
            ViewBag.thang2 = TongThanhtoanTrongThang(2);
            ViewBag.thang3 = TongThanhtoanTrongThang(3);
            ViewBag.thang4 = TongThanhtoanTrongThang(4);
            ViewBag.thang5 = TongThanhtoanTrongThang(5);
            ViewBag.thang6 = TongThanhtoanTrongThang(6);
            ViewBag.thang7 = TongThanhtoanTrongThang(7);
            ViewBag.thang8 = TongThanhtoanTrongThang(8);
            ViewBag.thang9 = TongThanhtoanTrongThang(9);
            ViewBag.thang10 = TongThanhtoanTrongThang(10);
            ViewBag.thang11 = TongThanhtoanTrongThang(11);
            ViewBag.thang12 = TongThanhtoanTrongThang(12);

            decimal tttTrongNgay = 0;
            decimal tttLai = 0;
            string strTableContent = string.Empty;
            var xuatkhos = db.XuatKhoes.Where(p => EntityFunctions.TruncateTime(p.NgayXuatKho) == EntityFunctions.TruncateTime(DateTime.Now)).OrderByDescending(p => p.MaHoaDon);
            var currentRole = Roles.GetRolesForUser(User.Identity.Name);
            foreach (XuatKho xk in xuatkhos)
            {
                //strTableCo = "<tr><td>" + xk.MaHoaDon + "</td><td>" + xk.KhachHang.TenKH + "</td><td>" + xk.KhachHang.DiaChiKH + "</td><td>" + xk.NhanVien.HoTen + "</td><td style='text-align:right'>" + xk.TongThanhToan + "</td>";
                if (currentRole.Contains("Administrator"))
                {
                    strTableContent += "<tr><td>" + xk.MaHoaDon + "</td><td>" + xk.KhachHang.TenKH + "</td><td>" + xk.KhachHang.DiaChiKH + "</td><td>" + xk.UserProfile.DisplayName + "</td><td style='text-align:right'>" + xk.TongThanhToan + "</td>" + "<td>" + xk.Lai + "</td></tr>";
                }
                else
                {
                    strTableContent += "<tr><td>" + xk.MaHoaDon + "</td><td>" + xk.KhachHang.TenKH + "</td><td>" + xk.KhachHang.DiaChiKH + "</td><td>" + xk.UserProfile.DisplayName + "</td><td style='text-align:right'>" + xk.TongThanhToan + "</td>" + "</tr>";
                }
                tttTrongNgay += Util.ConvertCurrencyToMoney(xk.TongThanhToan);
                tttLai += Util.ConvertCurrencyToMoney(xk.Lai);
            }
            ViewBag.tableContent = strTableContent;
            ViewBag.TongHD = Util.ConvertDecimaToCurrency(tttTrongNgay);
            ViewBag.TongLaiToday = Util.ConvertDecimaToCurrency(tttLai);

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
